#{extends 'main.html' /}
#{set title:'Show module' /}

<h2 class="ui-widget-header ui-widget ui-corner-all">${mod.id ? messages.get('module.edittitle') : messages.get('module.createtitle') }</h2>

#{ifErrors}
	<fieldset class="ui-corner-tl ui-corner-tr ui-state-error">&{'error.detected'}</fieldset>
#{/ifErrors}

<form action="@{Modules.save()}" id="savemod" enctype="multipart/form-data" method="POST" accept-charset="utf-8">
<fieldset class="ui-widget-content group">
	<legend>&{'module.group'}</legend>
	<table>
		<tr>
			<td width="150"><label for="mod.name">&{'module.name'}:</label></td>
			<td><input type="text" name="mod.name" value="${mod.name}" /></td>
			<td><span class="error">#{error 'mod.name' /}</span></td>
		</tr>
		<tr>
			<td><label for="mod.fullname">&{'module.fullname'}: </label></td>
			<td><input type="text" name="mod.fullname" value="${mod.fullname}" /></td>
		</tr>
		<tr>
			<td><label for="mod.license">&{'module.license'}: </label></td>
			%{def choices = models.LicenseType.values()}%
			<td><select type="text" name="mod.license">
				#{list items:choices, as:'choice'}
				<option value="${choice}" ${choice.equals(mod.license) ? 'selected="selected"'.raw() : ''}>&{choice.getDescriptionKey()}</option>
				#{/list}
			</select></td>
			<td><span class="error">#{error 'mod.license' /}</span></td>
		</tr>
	</table>
	<input type="text" class="ui-helper-hidden" name="id" value="${mod.id}" />
</fieldset>

#{if mod.id}
</form>
#{/if}

<fieldset class="ui-widget ui-widget-content group">
	<legend>&{'version.group'}</legend>
	%{def versions = mod.versions}%
	<div id="tabs" class="ui-widget-content ui-corner-all">
		<ul>
			#{list items:versions, as:'ver'}
			<li><a href="#tabs-${ver.id}">${ver.version}</a></li>
			#{/list}
			<li><a href="#tabs-new">&{'tabs.new'}</a></li>
		</ul>
		#{list items:versions, as:'ver'}
		<div id="tabs-${ver.id}" rel="${ver.id}" pos="${ver_index - 1}">
			<table>
				<tr>
					<td width="130"><label>&{'version.filename'}:</label></td>
					<td>${ver.filename}</td>
				</tr>
				<tr>
					<td><label>&{'version.isdefault'}:</label></td>
					<td>${ver.isDefault}</td>
				</tr>
				<tr>
					<td><label>&{'version.matches'}:</label></td>
					<td>${ver.matches}</td>
				</tr>
				<tr>
					<td><label>&{'version.date'}:</label></td>
					<td>${ver.date.format("yyyy/MM/dd HH:mm:ss")}</td>
				</tr>
				<tr>
					<td colspan="3" class="commands">
						<button id="button" rel="${ver.version}" class="button download">&{'button.load'}</button>
						<button id="button" rel="${ver.version}" class="button default">&{'button.default'}</button>
						<button id="button" rel="${ver.version}" class="button delete">&{'button.delete'}</button>
					</td>
				</tr>
			</table>
		</div>
		#{/list}
		<div id="tabs-new">
			#{if mod.id}
			<form action="@{Modules.addVersion()}" enctype="multipart/form-data" method="POST" accept-charset="utf-8">
			#{/if}
			<table>
				<tr>
					<td width="130"><label for="number">&{'version.version'}: </label></td>
					<td><input type="text" name="number" value="${number}" /></td>
					<td><span class="error">#{error 'number' /}</span></td>
				</tr>
				<tr>
					<td><label for="matches">&{'version.matches'}: </label></td>
					<td><input type="text" name="matches" value="${matches}" /></td>
					<td><span class="error">#{error 'matches' /}</span></td>
				</tr>
				<tr>
					<td><label for="archive">&{'version.archive'}: </label></td>
					<td><input type="file" name="archive" value="${archive}" /></td>
					<td><span class="error">#{error 'archive' /}</span></td>
				</tr>
				#{if mod.id}
				<tr>
					<td colspan="3" class="commands">
						<a id="button" href="#" class="button" onclick="javascript:$(this).parents('form:first').submit();">&{'button.add'}</a>
					</td>
				</tr>
				#{/if}
			</table>
			<input type="text" class="ui-helper-hidden" name="id" value="${mod.id}" />
			#{if mod.id}
			</form>
			#{/if}
		</div>
	</div>
</fieldset>

#{if mod.id == null}
</form>
#{/if}

<a href="@{Application.index()}" class="button">&{'button.back'}</a>
<a href="#" class="button" onclick="javascript:$('form#savemod').submit();">&{'button.save'}</a>

<div id="dialog-delete" title="&{'version.delete'}" class="ui-helper-hidden">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>&{'delversion.confirm'}</p>
</div>

<div class="ui-helper-hidden">
#{form @Modules.deleteVersion(), id:'delete'}
	<input type="hidden" name="id" value="${mod.id}" />
	<input type="hidden" name="number" value="" />
#{/form}
</div>

<div class="ui-helper-hidden">
#{form @Modules.download(), id:'download'}
	<input type="hidden" name="id" value="${mod.id}" />
	<input type="hidden" name="number" value="" />
#{/form}
</div>

<div class="ui-helper-hidden">
#{form @Modules.setDefault(), id:'default'}
	<input type="hidden" name="id" value="${mod.id}" />
	<input type="hidden" name="number" value="" />
#{/form}
</div>

<script>

$(function() {

	$("#tabs").tabs({
		show: function(event, ui) {
			if (ui.panel.id != "tabs-new") {
				if ($('#tabs').tabs().find('div').length == 2) {
					$('button.delete').hide();
				} else {
					$('button.delete').show();
				}
			}
		}
	});
	
	$('.button').button();
	
	$('button.download').click(function() {
		$('form#download input[type=hidden][name=number]:first').attr('value',$(this).attr('rel'));
		$('form#download')[0].submit();
	});
	
	$('button.default').click(function() {
		$('form#default input[type=hidden][name=number]:first').attr('value',$(this).attr('rel'));
		$('form#default')[0].submit();
	});
	
	$('button.delete').click(function() {
		// Ask for user confirmation
		$('form#delete input[type=hidden][name=number]:first').attr('value',$(this).attr('rel'));
		$('#dialog-delete').dialog('open');
		return false;
	});

	$("#dialog").dialog("destroy");

	$("#dialog-delete").dialog({
		resizable: false,
		height: "auto",
		modal: true,
		autoOpen: false,
		buttons: {
			'Delete': function() {
				$(this).dialog('close');
				// Send the selected model identifier (value) to the server
				$('form#delete')[0].submit();
			},
			Cancel: function() {
				$('form#delete input[type=hidden][name=number]:first').attr('value','');
				$(this).dialog('close');
			}
		}
	});

});

</script>
